module.exports = function ($scope, $state, getDate,
    $modal, ticketlist, createBackOrder, resend, getRedCorridorOrderList,
    getRedCorridorResentMsg, getRedCorridorTrSendSms, orderbacklist, relay,
    getOrderSimInfo, agencyOrderRepeatECode, updateTicketEffectTime, str2date,
    getroyalocOrdersState, testCreateBackOrder, categorylist, date2str, $resource
) {
    var ticketstate_arr = [
        {
            'label': '查询全部',
            'value': '',
        },
        {
            'label': '未出票',
            'value': '0',
        },
        {
            'label': '等待出票',
            'value': '1',
        },
        {
            'label': '出票完成',
            'value': '2',
        },
        {
            'label': '无需出票',
            'value': '8',
        },
        {
            'label': '出票失败',
            'value': '9',
        },

    ];
    $scope.tstate = ticketstate_arr;
    $scope.searchform = {

    };
    $scope.date = {
        'lable': date2str(new Date()),
        'value': date2str(new Date()),
        'opened': false
    }
    $scope.dateOpen = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };
    $scope.date1 = {
        'lable': date2str(new Date()),
        'value': date2str(new Date()),
        'opened': false
    }
    $scope.dateOpen1 = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };
    function date2str2(d) {
        if (d === undefined) {
            return "";
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

    /* 分页
     * ========================================= */
    $scope.maxSize = 5;            //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10;         //每页显示几条
    $scope.search = {
        'isselect': {
            'sale_category_name': ''
        }
    }
    $scope.getcategorylist = function () {
        categorylist.save({ 'type': 'sale_category' }, function (res) {
            if (res.errcode != 0) {
                alert(res.errmsg);
                return;
            }
            $scope.category_type = res.data;
            $scope.category_type.unshift({ label: '--全部查询--', value: '' });
        })
    }
    $scope.getcategorylist();
    $scope.change = function (code) {
        $scope.searchform.sale_category = code;
    }
    $scope.myKeyup = function (e) {
        //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.load();
        }
    };

    $scope.load = function () {
        if ($scope.date.lable) {
            if (typeof $scope.date.lable === 'string') {
                $scope.tour_date = $scope.date.lable
            } else {
                $scope.tour_date = date2str($scope.date.lable);
            }
        } else {
            $scope.tour_date = ''
        }
        if ($scope.date1.lable) {
            if (typeof $scope.date1.lable === 'string') {
                $scope.tour_date_two = $scope.date1.lable
            } else {
                $scope.tour_date_two = date2str($scope.date1.lable);
            }
        } else {
            $scope.tour_date_two = ''
        }
        if ($scope.date.lable != null && $scope.date1.lable != null) {
            if ($scope.tour_date > $scope.tour_date_two) {
                alert('开始日期不能大于结束日期');
                return;
            }
        }
        if ($scope.date.lable) {
            if (!$scope.date1.lable) {
                alert('开始和结束日期必须同时输入进行搜索')
                return;
            }
        }
        if ($scope.date1.lable) {
            if (!$scope.date.lable) {
                alert('开始和结束日期必须同时输入进行搜索')
                return;
            }
        }
        if ($scope.tour_date === '' || $scope.tour_date === undefined) {
            var para = {
                pageNo: $scope.bigCurrentPage,
                pageSize: $scope.itemsPerPage,
                start_time: '',
                end_time: '',
            };
        } else {
            var para = {
                pageNo: $scope.bigCurrentPage,
                pageSize: $scope.itemsPerPage,
                start_time: $scope.tour_date + " 00:00:00",
                end_time: $scope.tour_date_two + " 23:59:59",
            };
        }
        para = angular.extend($scope.searchform, para);
        $resource('/api/ac/tc/ticketOrderCartService/findOrderCartList', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                $scope.objs = res.data.results;
                $scope.bigTotalItems = res.data.totalRecord;
                $scope.realOrderList = [];
                $scope.cartMap = {};
                for (var index = 0; index < $scope.objs.length; index++) {
                    var element = $scope.objs[index];
                    if (element.order_cart_code) {  //此订单为购物车子订单
                        if ($scope.cartMap[element.order_cart_code] !== undefined) {    //相关购物车订单已经创建
                            $scope.realOrderList[$scope.cartMap[element.order_cart_code]].childList.push(element);
                        } else {    //相关购物车订单还没有创建
                            $scope.cartMap[element.order_cart_code] = $scope.realOrderList.length;  //相关购物车订单已经存在的位置
                            var child = angular.copy(element);
                            element.childList = [];
                            element.childList.push(child);
                            $scope.realOrderList.push(element)
                        }
                    } else {
                        var child = angular.copy(element);
                        element.childList = [];
                        element.childList.push(child);
                        $scope.realOrderList.push(element)
                    }
                }
                console.log($scope.realOrderList)

            } else {
                alert(res.errmsg);
            }
        });
    };
    $scope.load();

    $scope.ticketlist = function (obj) {
        var modalInstance = $modal.open({
            template: require('../views/orderticketlist.html'),
            controller: 'orderticketlist',
            size: 'lg',
            resolve: {
                obj: function () {
                    return obj;
                },
                ticketlist: function () {
                    return ticketlist;
                },
                createBackOrder: function () {
                    return createBackOrder;
                },
                //红海滩
                getRedCorridorOrderList: function () {
                    return getRedCorridorOrderList;
                },
                //退票历史
                orderbacklist: function () {
                    return orderbacklist;
                },
                //北京票联  红海滩廊道
                getOrderSimInfo: function () {
                    return getOrderSimInfo;
                },
                //修改生效时间
                updateTicketEffectTime: function () {
                    return updateTicketEffectTime;
                },
                getDate: function () {
                    return getDate;
                },
                str2date: function () {
                    return str2date;
                },
                getroyalocOrdersState: function () {
                    return getroyalocOrdersState;
                }
            }
        });
        modalInstance.result.then(function () {
        }, function () {
        });

    };

    //打开模态框
    $scope.orderinfo = function (obj) {

        var modalInstance = $modal.open({
            template: require('../views/orderinfo.html'),
            controller: 'orderinfo11',
            size: 'lg',
            resolve: {
                obj: function () {
                    return obj;
                }
            }
        });
        modalInstance.result.then(function () {
        }, function () {
        });
    }

    $scope.resend = function (obj) {
        var fun;
        var para = {};
        if (obj.sale_belong === 'juyou' || obj.sale_belong.indexOf('supply_piaofutong') == 0 || obj.sale_belong.indexOf('supply_tstc') == 0 || obj.sale_belong === 'supply_tongchenglvyou' || obj.sale_belong === 'supply_zhiyoubao') {
            fun = resend;
            para['code'] = obj.code;
        }
        else if (obj.sale_belong == 'langdao') {
            fun = getRedCorridorResentMsg;
            para['code'] = obj.code;
        }
        else if (obj.sale_belong == 'huaxiapiaolian') {
            fun = agencyOrderRepeatECode;
            para['order_code'] = obj.code;
            para['ownerMobile'] = obj.mobile;
        }
        fun.save(para, function (res) {
            if (res.errcode === 0) {
                alert('发送成功');
            }
            else {
                alert(res.errmsg);
            }
        });
    }

    $scope.relay = function (obj) {
        var modalInstance = $modal.open({
            template: require('../views/relaymessage.html'),
            controller: 'relaymessage',
            size: 'xs',
            resolve: {
                code: function () {
                    return obj.code;
                },
                relay: function () {
                    return relay;
                },
                getRedCorridorTrSendSms: function () {
                    return getRedCorridorTrSendSms;
                },
                sale_belong: function () {
                    return obj.sale_belong;
                },
                agencyOrderRepeatECode: function () {
                    return agencyOrderRepeatECode;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
        });
    }

    $scope.back = function (obj, flag) {
        var modalInstance = $modal.open({
            template: require('../views/backticket.html'),
            controller: 'backticket',
            size: 'xs',
            resolve: {
                code: function () {
                    return obj.code;
                },
                num: function () {
                    return obj.num;
                },
                createBackOrder: function () {
                    return createBackOrder;
                },
                flag: function () {
                    return flag;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
        });
    }

    $scope.testBack = function (obj) {
        var modalInstance = $modal.open({
            template: require('../views/backticket.html'),
            controller: 'backticket',
            size: 'xs',
            resolve: {
                code: function () {
                    return obj.code;
                },
                num: function () {
                    return obj.num;
                },
                createBackOrder: function () {
                    return testCreateBackOrder;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
        });
    }
};